import { networkInterfaces, NetworkInterfaceInfo } from 'os';

/**
 * 获取本地IPv4地址
 * @returns {string} 返回本地IPv4地址，如果没有找到则返回'localhost'
 */
export const getLocalIp = (): string => {
  const nets = networkInterfaces();
  const results: NetworkInterfaceInfo[] = Object.keys(nets)
    .map(name => nets[name])
    .filter((net): net is NetworkInterfaceInfo[] => net !== undefined)
    .flat();

  const ip = results.find(net => 
    net.family === 'IPv4' && !net.internal
  );

  return ip ? ip.address : 'localhost';
};